<template>
    <a-empty v-if="columns.length == 0" description="请选择计费类型"></a-empty>
    <ma-form
        v-model="formModel.price_config"
        :options="{showButtons: false}"
        :columns="columns"
    />
</template>

<script setup>
import {inject, ref, watch,onMounted} from "vue";
import {isEmpty, get, isObject} from "lodash";

const formModel = inject("formModel")

onMounted(() => {
  setColumns(formModel.value.type,'')
});


/**
 *
 * @param a1 集装箱
 * @param a2 整车
 * @returns {*}
 */
const suffixTextGet = (a1, a2) => {
  return formModel.value.transport_type == 1 ? a1:a2
}


const getFormColumns = () => {
  if (formModel.value.transport_type == 2){
    return {
      // 装卸费
      1: [
        {
          dataIndex: "price",
          title: "单价",
          formType: "input-number",
          suffixText: suffixTextGet("元/箱", "元/吨"),
          rules: {
            required: true,
            message: "请输入单价"
          }
        }
      ],
      // 出库费
      2: [
        {
          dataIndex: "price",
          title: "单价",
          formType: "input-number",
          suffixText: "元/吨",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
      ],
      // 配送费
      3: [
        {
          dataIndex: "price",
          title: "单价",
          formType: "input-number",
          suffixText: "元/车",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
      ],
      // 仓储费
      4: [
        {
          dataIndex: "price",
          title: "单价",
          formType: "input-number",
          suffixText: "元/吨/天",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
        {
          dataIndex: "overtime_day",
          title: "阶梯计费天数",
          formType: "input-number",
          suffixText: "天",
          rules: {
            required: true,
            message: "请输入阶梯计费天数"
          }
        },
        {
          dataIndex: "free_day",
          title: "免费天数",
          formType: "input-number",
          suffixText: "天",
          rules: {
            required: true,
            message: "请输入免费天数"
          }
        }
      ],
      // 专线使用费
      5: [
        {
          dataIndex: "price",
          title: "单价",
          formType: "input-number",
          suffixText: "元/吨",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
      ],
      // 暂存费
      6: [
        {
          dataIndex: "price",
          title: "单价",
          formType: "input-number",
          suffixText: "元/车/天",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
        {
          dataIndex: "free_day",
          title: "免费天数",
          formType: "input-number",
          suffixText: "天",
          rules: {
            required: true,
            message: "请输入免费天数"
          }
        }
      ],
      // 过磅费
      7: [
        {
          dataIndex: "price",
          title: "单价",
          formType: "input-number",
          suffixText: suffixTextGet("元/箱", "元/吨"),
          rules: {
            required: true,
            message: "请输入单价"
          }
        }
      ],
      // 一口价
      8: [
        {
          dataIndex: "price",
          title: "单价",
          formType: "input-number",
          suffixText: suffixTextGet("元/箱", "元/吨"),
          rules: {
            required: true,
            message: "请输入单价"
          }
        }
      ]
    }
  } else if (formModel.value.transport_type == 1 && formModel.value.is_taoxiang == 0) {
    return {
      // 装卸费
      1: [
        {
          dataIndex: "20_price",
          title: "20英尺单价",
          formType: "input-number",
          suffixText: "元/20英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
        {
          dataIndex: "40_price",
          title: "40英尺单价",
          formType: "input-number",
          suffixText: "元/40英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        }
      ],
      // 出库费
      2: [
        {
          dataIndex: "20_price",
          title: "20英尺单价",
          formType: "input-number",
          suffixText: "元/20英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
        {
          dataIndex: "40_price",
          title: "40英尺单价",
          formType: "input-number",
          suffixText: "元/40英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        }
      ],
      // 配送费
      3: [
        {
          dataIndex: "20_price",
          title: "20英尺单价",
          formType: "input-number",
          suffixText: "元/20英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
        {
          dataIndex: "40_price",
          title: "40英尺单价",
          formType: "input-number",
          suffixText: "元/40英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        }
      ],
      // 仓储费
      4: [
        {
          dataIndex: "20_price",
          title: "20英尺单价",
          formType: "input-number",
          suffixText: "元/20英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
        {
          dataIndex: "40_price",
          title: "40英尺单价",
          formType: "input-number",
          suffixText: "元/40英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
        {
          dataIndex: "overtime_day",
          title: "阶梯计费天数",
          formType: "input-number",
          suffixText: "天",
          rules: {
            required: true,
            message: "请输入阶梯计费天数"
          }
        },
        {
          dataIndex: "free_day",
          title: "免费天数",
          formType: "input-number",
          suffixText: "天",
          rules: {
            required: true,
            message: "请输入免费天数"
          }
        }
      ],
      // 专线使用费
      5: [
        {
          dataIndex: "price",
          title: "单价",
          formType: "input-number",
          suffixText: "元/吨",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
      ],
      // 暂存费
      6: [
        {
          dataIndex: "price",
          title: "单价",
          formType: "input-number",
          suffixText: "元/车/天",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
        {
          dataIndex: "free_day",
          title: "免费天数",
          formType: "input-number",
          suffixText: "天",
          rules: {
            required: true,
            message: "请输入免费天数"
          }
        }
      ],
      // 过磅费
      7: [
        {
          dataIndex: "price",
          title: "单价",
          formType: "input-number",
          suffixText: '元/吨',
          rules: {
            required: true,
            message: "请输入单价"
          }
        }
      ],
      8: [
        {
          dataIndex: "20_price",
          title: "20英尺单价",
          formType: "input-number",
          suffixText: "元/20英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
        {
          dataIndex: "40_price",
          title: "40英尺单价",
          formType: "input-number",
          suffixText: "元/40英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
      ],
    }
  } else if (formModel.value.transport_type == 1 && formModel.value.is_taoxiang == 1){
    return {
      // 装卸费
      1: [
        {
          dataIndex: "20_price",
          title: "20英尺单价",
          formType: "input-number",
          suffixText: "元/20英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
        {
          dataIndex: "40_price",
          title: "40英尺单价",
          formType: "input-number",
          suffixText: "元/40英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        }
      ],
      // 出库费
      2: [
        {
          dataIndex: "20_price",
          title: "20英尺单价",
          formType: "input-number",
          suffixText: "元/20英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
        {
          dataIndex: "40_price",
          title: "40英尺单价",
          formType: "input-number",
          suffixText: "元/40英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        }
      ],
      // 配送费
      3: [
        {
          dataIndex: "20_price",
          title: "20英尺单价",
          formType: "input-number",
          suffixText: "元/20英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
        {
          dataIndex: "40_price",
          title: "40英尺单价",
          formType: "input-number",
          suffixText: "元/40英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        }
      ],
      // 仓储费
      4: [
        {
          dataIndex: "20_price",
          title: "20英尺单价",
          formType: "input-number",
          suffixText: "元/20英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
        {
          dataIndex: "40_price",
          title: "40英尺单价",
          formType: "input-number",
          suffixText: "元/40英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
        {
          dataIndex: "overtime_day",
          title: "阶梯计费天数",
          formType: "input-number",
          suffixText: "天",
          rules: {
            required: true,
            message: "请输入阶梯计费天数"
          }
        },
        {
          dataIndex: "free_day",
          title: "免费天数",
          formType: "input-number",
          suffixText: "天",
          rules: {
            required: true,
            message: "请输入免费天数"
          }
        }
      ],
      // 专线使用费
      5: [
        {
          dataIndex: "price",
          title: "单价",
          formType: "input-number",
          suffixText: "元/吨",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
      ],
      // 暂存费
      6: [
        {
          dataIndex: "price",
          title: "单价",
          formType: "input-number",
          suffixText: "元/吨/天",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
        {
          dataIndex: "free_day",
          title: "免费天数",
          formType: "input-number",
          suffixText: "天",
          rules: {
            required: true,
            message: "请输入免费天数"
          }
        }
      ],
      // 过磅费
      7: [
        {
          dataIndex: "price",
          title: "单价",
          formType: "input-number",
          suffixText: '元/吨',
          rules: {
            required: true,
            message: "请输入单价"
          }
        }
      ],
      8: [
        {
          dataIndex: "20_price",
          title: "20英尺单价",
          formType: "input-number",
          suffixText: "元/20英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
        {
          dataIndex: "40_price",
          title: "40英尺单价",
          formType: "input-number",
          suffixText: "元/40英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
      ],
      9: [
        {
          dataIndex: "20_price",
          title: "20英尺单价",
          formType: "input-number",
          suffixText: "元/20英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
        {
          dataIndex: "40_price",
          title: "40英尺单价",
          formType: "input-number",
          suffixText: "元/40英尺箱",
          rules: {
            required: true,
            message: "请输入单价"
          }
        },
      ]
    }
  }
}
const columns = ref([])
const setColumns = (value, oldValue) => {
  if (isEmpty(value)) {
    return;
  }
  if (!isObject(formModel.value.price_config)) {
    formModel.value.price_config = {}
  }
  let columns_get = getFormColumns()
  columns.value = get(columns_get, value, [])
  let object = {}
  columns.value.forEach(item => {
    object[item.dataIndex] = item.suffixText
  })
  formModel.value.price_config.text = object
}


watch(() => formModel.value.type, (value, oldValue) => {
  setColumns(value, oldValue)
})

</script>

<style scoped>

</style>
